<?
include "/var/www/html/new/d/module/qkxproject/api/des/des.class.php";
class FeiCD
{
    public  $_pid="";
    public  $authcode = "";
    private  $url='http://bridge.piao58.cn/PlatForm/API?';
   public  function  __construct($UUlid){
       switch($UUlid){
           case 9290:
             $this->_pid = '15060049363';//百合国旅
             $this->authcode ='d272cd151cd7bc2bb2205f7f';
              break;
           case 10423:
               $this->_pid = '13780366666';//海源账号
               $this->authcode ='f845cc151cd7bc2ab1635f7f';
               break;
           default;
               break;
       }
   }

    public function  curl_post($url, $post_data){//POST数据提交
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }

    public function Agent_AddOrder($request_array){//订单提交
        $request = array(
            "_pid"=>$this->_pid,
            "_ts"=>time(),
            "format"=>'xml',
            "MobileNumberToGetEticket"=>$request_array['UUordertel'],//电话号码
            "OrderKeyId"=>$request_array['pftordernum'],//票付通订单号
            "productVersionKeyId"=>$request_array['uuid'],//远端产品id
            "ProductVersionPrice"=>$request_array['UUlprice'],//供货价格
            "TripDate"=>$request_array['UUplaytime'],//游玩日期
            "TotalTicketQuantity"=>$request_array['Tnum'],//游玩人数
            );

        $post_data =$this->asort_sign($request);//数据排序
        $url = $this->url.'method=Agent_AddOrder';
        $post_request = $this->curl_post($url,$post_data);
        $request_data_xml = simplexml_load_string($post_request);
        $body = $request_data_xml->Body;
        $crypt= new Crypt3Des($this->authcode);//返回数据解密
        $des = $crypt->decrypt($body);
        $respone_orderid_xml =  simplexml_load_string($des);
        $respone_orderid =(string)$respone_orderid_xml->OrderId;
        $response_message = (string)$request_data_xml->Head->StatusCode;
        if($response_message==100){
            $ret = $this->Agent_OrderPay($respone_orderid);//调用支付接口
             if ($ret==100){
                   /*  if (!class_exists("go_sql")) {
                         include("/var/www/html/new/conf/le.je");
                         $le = new go_sql();
                         $le->connect();
                     }
                 $str = "update uu_order_fx_details set memo='fcd_pay_success' where orderid=".$request_array['pftordernum']." limit 1";
                 $GLOBALS['le']->query($str);*/
                 return $request = (string)$respone_orderid;
             }else{
                 $this->logfile(date('Y-m-d H:i:s').'---'.$request_array['pftordernum']."->".$response_message,'/var/www/html/new/d/module/qkxproject/api/feicd/feicd_log.txt');
                 return 102;
            }
        }else{
            $this->logfile(date('Y-m-d H:i:s').'---'.$request_array['pftordernum']."->".$response_message,'/var/www/html/new/d/module/qkxproject/api/feicd/feicd_log.txt');
            return 101;
        }
    }

    public function Agent_OrderPay($respone_orderid){//支付接口
//传输数据
        $request = array("_pid"=>$this->_pid,
            "_ts"=>time(),
            "format"=>'xml',
            "orderKeyId"=>$respone_orderid,
            "payType"=>64,
           );

        $post_data =$this->asort_sign($request);
        $url = $this->url.'method=Agent_OrderPay';
        $post_request = $this->curl_post($url,$post_data);
        $request_data_xml = simplexml_load_string($post_request);
        $response_message = (string)$request_data_xml->Head->StatusCode;
        if($response_message==100){
            return $response_message;
        }else{
            $this->logfile(date('Y-m-d H:i:s').'StatusCode'.$response_message,'/var/www/html/new/d/module/qkxproject/api/feicd/feicd_log.txt');
            return 101;
        }
    }

    public  function  Agent_OrderRefund($arr_refund){
        $request = array("_pid"=>$this->_pid,
            "_ts"=>time(),//当前时间
            "format"=>'xml',//传输格式
            "orderKeyId"=>$arr_refund['UUremotenum'],//远端订单号-对方平台订单号
            "Num"=>$arr_refund['UUtnum'],//购票人数
            "ParnterId"=>$arr_refund['orders'],//票付通订单号
            "ProductVersionKeyId"=>$arr_refund['uuid'],//产品id
        );
        $post_data =$this->asort_sign($request);
        $url = $this->url.'method=Agent_OrderRefund';
        $post_request = $this->curl_post($url,$post_data);
        $request_data_xml = simplexml_load_string($post_request);
        $response_message = (string)$request_data_xml->Head->StatusCode;
    if($response_message==100){
        return 100;
    }else{
        $this->logfile(date('Y-m-d H:i:s').'StatusCode'.$response_message,'/var/www/html/new/d/module/qkxproject/api/feicd/feicd_refund.txt');
        return 101;
    }
    }
    //函数部分
    public function asort_sign($array){
        foreach($array as $k => $v){
            $array[strtolower($k)] = $v;
            if(strtolower($k) != $k)
                unset($array[$k]);
        }
        ksort($array);
        $other="";
        foreach ($array as $k=>$v){
            $other .= strtolower($k)."=".$v."&";
        }
//         print_r($other);
//         exit;
        $other = substr($other,0,-1);
        $_sig = md5($other.$this->authcode);
        $post_data = $other.'&_sign='.$_sig;
        return $post_data;

    }
    function logfile($txt, $path)
    {
        $fp = fopen($path, "a");
        flock($fp, LOCK_EX);
        fwrite($fp, $txt . "\n");
        flock($fp, LOCK_UN);
        fclose($fp);
    }
}

?>